import * as Koa from 'koa'
import { ApolloServer } from 'apollo-server-koa'
import { GraphQLSchema } from 'graphql'
import bootstrapConnection from './db'
import { makeExecutableSchema as makeSchema } from 'graphql-tools'
import typeDefs from './schema'
import resolvers from './resolvers'


void (async () => {
  try {
    const db = await bootstrapConnection()

    const schema: GraphQLSchema = makeSchema({
      typeDefs,
      resolvers
    })
    const server = new ApolloServer({
      schema,
      context: ({ req }) => {
        return { ...req, db }
      },
    })

    const app = new Koa()
    server.applyMiddleware({ app })

    app.listen({ port: 4000 }, () =>
      console.log(`🚀 Server ready at http://localhost:4000${server.graphqlPath}`),
    )
  } catch (e) {
    console.log(e)
  }
})()

